//
//  CIBNAVPlayerManager.m
//  CIBN
//
//  Created by ZNGOO on 16/7/20.
//  Copyright © 2016年 韦儒健. All rights reserved.
//

#import "CIBNAVPlayerManager.h"

@interface CIBNAVPlayerManager()

@property (nonatomic,strong) AVPlayerItem * playerItem;
@property (nonatomic,strong) AVPlayerLayer * playerlayer;
@end

@implementation CIBNAVPlayerManager

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

- (void)setVideoUrl:(NSString *)url{
    
    self.playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    
    if (self.player == nil) {
        self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem];
    }
    else{
        [self.player replaceCurrentItemWithPlayerItem:self.playerItem];
    }
    
    
    // 计算缓冲
    __strong __typeof(self)weakSelf = self;
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        
        NSArray * range = weakSelf.player.currentItem.loadedTimeRanges;
        NSLog(@"%@",range);
//        loadedTimeRanges.firstObject
    }];
    
    
    // 设置大小
    
    self.playerlayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.playerlayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    self.playerlayer.videoGravity = AVLayerVideoGravityResizeAspect;
    [self.layer addSublayer:self.playerlayer];
    
//    
//    UIButton * back = [[UIButton alloc] initWithFrame:CGRectMake(5, 12, 40, 40)];
//    [back setImage:[UIImage imageNamed:@"movie_back_s"] forState:UIControlStateNormal];
//    [self addSubview:back];
    
}

- (void)videoPlay{
    
    [self.player play];
}

- (void)videoPause{
    [self.player pause];
}

- (void)isfulScreen{
    [UIView animateWithDuration:0.5 animations:^{
       self.playerlayer.frame = self.bounds;
    }];
    
}
@end
